<template>
  <div class="select">
    <div class="search">
      <input type="text" v-model="val" placeholder="请输入搜索关键词" />
      <button @click="searchcontent" @keydown.enter="searchcontent">
        搜索
      </button>
    </div>
    <ul class="search-list">
      <li v-for="item in list" :key="item.id">
        <div class="search-img">
          <img :src="item.image" />
        </div>
        <div class="search-list-info">
          <p class="search-title">{{ item.title }}</p>
          <div class="search-info">
            <span class="search-price">￥{{ item.price }}</span>
            <span class="search-sale">{{ item.saleNum }}人已买</span>
          </div>
        </div>
      </li>
    </ul>
    <!-- 回到顶部 -->
    <ToTop />
  </div>
</template>

<script>
import ToTop from "../components/ToTop";
export default {
  name: "Select",
  data() {
    return {
      list: [],
      val: ""
    };
  },
  components: {
    ToTop
  },
  methods: {
    searchcontent() {
      // console.log(11);
      if (!this.val.trim()) return;
      this.$http.getSearch(this.val).then(resp => {
        //console.log(resp);
        this.list = resp.list;
      });
    }
  }
};
</script>

<style lang="scss">
.select {
  height: 100%;
  overflow: auto;
  .search {
    width: 100%;
    margin-top: 20px;
    input {
      margin-left: 30px;
      width: 250px;
      height: 30px;
      border: 2px solid #31a5fc;
      outline: none;
      border-radius: 15px;
      padding-left: 20px;
      box-sizing: border-box;
      color: #000;
    }
    button {
      width: 50px;
      margin-left: 10px;
      border-radius: 5px;
      background: #31a5fc;
      border: 2px solid #31a5fc;
      color: #fff;
    }
  }
}
.search-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  background: #f2f2f2;
  li {
    width: 49%;
    background: #fff;
    box-shadow: 0 1px 1px -1px rgba(0, 0, 0, 0.5);
    margin-top: 10px;
  }
  .search-img {
    overflow: hidden;
    position: relative;
    padding-top: 100%;
    width: 100%;
    display: block;
    img {
      position: absolute;
      top: 50%;
      left: 50%;
      height: 100%;
      display: block;
      transform: translate(-50%, -50%);
    }
  }
  .search-list-info {
    padding: 0 5px;
  }
  .search-title {
    overflow: hidden;
    font-size: 12px;
    line-height: 24px;
    text-overflow: ellipsis;
    color: #877a73;
    white-space: nowrap;
  }
  .search-price {
    color: #fa585a;
    font-weight: 400;
    font-size: 16px;
    line-height: 32px;
  }
  .search-sale {
    font-size: 12px;
    color: #877a73;
    letter-spacing: 0;
    margin: 10px;
  }
}
</style>
